from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.binance.kr_rest.constants import quote_currency_list
from coin.exchange.binance.config import group_symbol
from coin.exchange.binance.kr_rest.currency import BinanceCurrency


class BinanceProduct(NativeProduct):
  _exchange = "Binance"
  NativeCurrencyType = BinanceCurrency

  def __init__(self, base, quote):
    assert base != quote
    assert quote.currency in quote_currency_list, quote
    super().__init__(base, quote, "%s%s" % (base.native_currency, quote.native_currency))

  @classmethod
  def FromStrProductImpl(cls, product, current_datetime):
    assert type(product) == str, type(product)
    assert '-' in product, product
    pair = product.split('-')
    assert len(pair) == 2
    base, quote = pair
    return BinanceProduct.FromStrPair(base, quote, current_datetime)

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert type(product) == str
    # Exception for native symbol
    if product == "ETC":
      return BinanceProduct.FromStrPairNative("ETC", "456")

    if '_' in product:
      pair = product.split('_')
      assert len(pair) == 2
      base, quote = pair
      return BinanceProduct.FromStrPairNative(base, quote, current_datetime)
    else:
      for quote in quote_currency_list:
        if product.endswith(quote):
          base = product[:-len(quote)]
          return BinanceProduct.FromStrPairNative(base, quote, current_datetime)
    raise ValueError('Invalid native product: %s' % product)
